home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
cmprss.exe
/
COMPRESS.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-01-26
|
2KB
|
121 lines
#include "compress.cls"
cpofstream::cpofstream(void) : idx(0)
{
}
cpofstream::cpofstream(char *fn) : idx(0)
{
init(); // allocate space for buffers
ofstream::open(fn, ios::binary | ios::out);
}
cpofstream::~cpofstream()
{
flush();
clear();
}
void cpofstream::fillbuf(void* v, int l)
{
while (l--)
{
inbuf[idx]=*(char*)v;
if (++idx==CBUF_SIZE)
encode();
((char*)v)++;
}
}
cpofstream& cpofstream::operator<<(char *s)
{
while (*s)
{
inbuf[idx]=*s;
if (++idx==CBUF_SIZE)
encode();
s++;
}
inbuf[idx]=*s; // have to do NULL also
if (++idx==CBUF_SIZE)
encode();
return *this;
}
cpofstream& cpofstream::operator<<(void* v)
{
unsigned long l=(unsigned long)v;
fillbuf(&l,sizeof(unsigned long));
return *this;
}
void cpofstream::open(char *fn)
{
init(); // allocate space for buffers
ofstream::open(fn, ios::binary | ios::out);
}
filebuf* cpofstream::close(void)
{
flush();
clear(); // deallocate buffers
return rdbuf()->close();
}
cpofstream& cpofstream::flush(void)
{
if (idx)
{
encode();
}
ofstream::write((char*)&idx,sizeof(idx)); // write EO(compressed)F
reset();
return *this;
}
void cpofstream::encode(void)
{
int n=compress(inbuf, idx, outbuf, hash_table, HASH_LEN);
ofstream::write((char*)&n,sizeof(n));
if (n<0)
n=-n;
ofstream::write(outbuf,n);
idx=0;
}
void cpofstream::init(void)
{
inbuf=new unsigned char[CBUF_SIZE];
outbuf=new unsigned char[CBUF_SIZE];
hash_table=new unsigned char*[HASH_LEN];
reset();
}
void cpofstream::clear(void)
{
delete inbuf;
delete outbuf;
delete hash_table;
}
void cpofstream::reset(void)
{
for (int i=0;i<HASH_LEN;i++)
{
hash_table[i]=0L;
}
}